package com.sxran.im.feign;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.sxran.im.feign.dto.ImGroupDto;
import com.sxran.im.feign.dto.ImSuperDto;
import com.sxran.im.sbean.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ImApiFeign {
    /**
     * 给一批人发消息
     * @param userIdArray
     * @param data
     */
    public void batchChatUser(List<String> userIdArray , ImSuperDto data ){
        BatchChatUserInputBean chatUserInputBean = new BatchChatUserInputBean();
        chatUserInputBean.setUserIdArray(userIdArray);
        chatUserInputBean.setData(data);
        post("batch-chat-user",chatUserInputBean);
    }

    /**
     * 为单个人发送消息
     * @param userId
     * @param data
     */
    public void chatUser(String userId , ImSuperDto data ){
        ChatUserInputBean chatUserInputBean = new ChatUserInputBean();
        chatUserInputBean.setUserId(userId);
        chatUserInputBean.setData(data);
        post("chat-user",chatUserInputBean);
    }

    /**
     * 向群组发送消息
     * @param groupDto
     */
    public void chatGroup(ImGroupDto groupDto ){
        ChatGroupInputBean chatGroupInputBean = new ChatGroupInputBean();
        chatGroupInputBean.setGroup(groupDto.getGroupId());
        chatGroupInputBean.setData(groupDto);
        post("chat-group",chatGroupInputBean);
    }

    /**
     * 给userId绑定群组
     * @param userBindGroupInputBean
     */
    public void bindGroupByUser( UserBindGroupInputBean userBindGroupInputBean ){
        post("user-bind-group",userBindGroupInputBean);
    }

    /**
     * 给userId解绑群组
     * @param userBindGroupInputBean
     */
    public void unbindGroupByUser( UserUnbindGroupInputBean userBindGroupInputBean ){
        post("user-unbind-group",userBindGroupInputBean);
    }

    private void post(String uri , Object req){
        HttpUtil.post("127.0.0.1:8080/"+uri, JSONUtil.toJsonStr(req),1000*5);
    }

}
